//
//  lxGoodsDetailView.m
//  luodansc
//
//  Created by 倫 星 on 2012/10/09.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxGoodsDetailView.h"
#import "lxImageView.h"
#import "lxLabel.h"
#import "lxGoodsDetailEntity.h"
#import <QuartzCore/QuartzCore.h>

@implementation lxGoodsDetailView
@synthesize img_goods_view = _img_goods_view;
@synthesize lb_market_price_lb = _lb_market_price_lb;
@synthesize lb_shop_price_lb = _lb_shop_price_lb;
@synthesize lb_promote_price_lb = _lb_promote_price_lb;
@synthesize lb_comment_rank_lb = _lb_comment_rank_lb;

@synthesize lb_market_price = _lb_market_price;
@synthesize lb_shop_price = _lb_shop_price;
@synthesize lb_promote_price = _lb_promote_price;
@synthesize image_comment_rank_img = _image_comment_rank_img;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (nil != self) {
        self.frame = frame;
        
        float width = self.bounds.size.width;
        float lb_height = self.bounds.size.height / 38;
        
        float lb_price_width = width / 4;
        if (ISIPADDEVICE) {
            lb_price_width = width / 8;
        }
        
        float price_width = width / 8;
        if (ISIPADDEVICE) {
            price_width = width / 16;
        }
        
        float left_space = width / 8;
        if (ISIPADDEVICE) {
            left_space = width / 16;
        }
        
        CGRect ret_gv = CGRectMake(0, lb_height, width, lb_height * 20);
        _img_goods_view = [[lxImageView alloc] initWithFrame:ret_gv];
        //圆角
        [_img_goods_view.layer setMasksToBounds:YES];
        [_img_goods_view.layer setCornerRadius:15];
        
        CGRect ret_mp_lb = CGRectMake(left_space, lb_height * 22, lb_price_width, lb_height * 2);
        _lb_market_price_lb = [[lxLabel alloc] initWithFrame:ret_mp_lb];
        _lb_market_price_lb.text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"market_price", nil)];
        _lb_market_price_lb.textAlignment = NSTextAlignmentRight;
        _lb_market_price_lb.adjustsFontSizeToFitWidth = YES;
        
        
        CGRect ret_mp = CGRectMake(left_space + lb_price_width, lb_height * 22, price_width, lb_height * 2);
        _lb_market_price = [[lxLabel alloc] initWithFrame:ret_mp];
        _lb_market_price.textAlignment = NSTextAlignmentLeft;
        _lb_market_price.adjustsFontSizeToFitWidth = YES;
        
        CGRect ret_sp_lb = CGRectMake(left_space, lb_height * 24, lb_price_width, lb_height * 2);
        _lb_shop_price_lb = [[lxLabel alloc] initWithFrame:ret_sp_lb];
        _lb_shop_price_lb.textAlignment = NSTextAlignmentRight;
        _lb_shop_price_lb.adjustsFontSizeToFitWidth = YES;
        _lb_shop_price_lb.text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"shop_price", nil)];
        
        CGRect ret_sp = CGRectMake(left_space + lb_price_width, lb_height * 24, price_width, lb_height * 2);
        _lb_shop_price = [[lxLabel alloc] initWithFrame:ret_sp];
        _lb_shop_price.textAlignment = NSTextAlignmentLeft;
        _lb_shop_price.adjustsFontSizeToFitWidth = YES;
        
        CGRect ret_pp_lb = CGRectMake(left_space, lb_height * 26, lb_price_width, lb_height * 2);
        _lb_promote_price_lb = [[lxLabel alloc] initWithFrame:ret_pp_lb];
        _lb_promote_price_lb.textAlignment = NSTextAlignmentRight;
        _lb_promote_price_lb.adjustsFontSizeToFitWidth = YES;
        _lb_promote_price_lb.text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"promote_price", nil)];
        
        CGRect ret_pp = CGRectMake(left_space + lb_price_width, lb_height * 26, price_width, lb_height * 2);
        _lb_promote_price = [[lxLabel alloc] initWithFrame:ret_pp];
        _lb_promote_price.textAlignment = NSTextAlignmentLeft;
        _lb_promote_price.adjustsFontSizeToFitWidth = YES;
        
        CGRect ret_cr_lb = CGRectMake(left_space, lb_height * 28, lb_price_width, lb_height * 2);
        _lb_comment_rank_lb = [[lxLabel alloc] initWithFrame:ret_cr_lb];
        _lb_comment_rank_lb.textAlignment = NSTextAlignmentRight;
        _lb_comment_rank_lb.adjustsFontSizeToFitWidth = YES;
        _lb_comment_rank_lb.text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"comment_rank", nil)];
        
        float image_start_height = lb_height * 28.5;
        if (ISIPADDEVICE) {
            image_start_height = lb_height * 28.8;
        }
        
        CGRect ret_image_comment_rank_img = CGRectMake(left_space + lb_price_width, image_start_height, width - (left_space + lb_price_width), lb_height * 2);
        _image_comment_rank_img = [[lxImageView alloc] initWithFrame:ret_image_comment_rank_img];
        [_image_comment_rank_img setLxAlignmentCenter:NO];
        [_image_comment_rank_img setUserInteractionEnabled:NO];
        
        [self addSubview:_img_goods_view];
        [self addSubview:_lb_market_price_lb];
        [self addSubview:_lb_shop_price_lb];
        [self addSubview:_lb_promote_price_lb];
        [self addSubview:_lb_comment_rank_lb];
        [self addSubview:_lb_market_price];
        [self addSubview:_lb_shop_price];
        [self addSubview:_lb_promote_price];
        [self addSubview:_image_comment_rank_img];
    }
    return self;
}

- (BOOL)refreshData:(lxbaseEntity *)entity {
    BOOL result = [super refreshData:entity];
    if (NO == result) {
        return result;
    }
    
    lxGoodsDetailEntity *lxentity = (lxGoodsDetailEntity*)entity;
    NSString *goods_img = lxentity.goods_img;
    
    if (ISIPADDEVICE) {
        goods_img = lxentity.original_img;
    }
    
    if (nil != goods_img) {
        NSString *str_url = [NSString stringWithFormat:@"%@%@", web_root, goods_img];
        
        [_img_goods_view loadImageFromURL:str_url];
    }
    
    NSString *money = NSLocalizedString(@"money", nil);
    
    NSString *str_market_price = [lxStringUtility stringTrim:[NSString stringWithFormat:@"%10.2f", lxentity.market_price]];
    if (NO == [lxStringUtility isNil:str_market_price]) {
        _lb_market_price.textColor = [UIColor grayColor];
        _lb_market_price.isWithStrikeThrough = YES;
    } else {
        str_market_price = @"0";
    }
    _lb_market_price.text = [NSString stringWithFormat:@"%@%@", str_market_price, money];
    
    NSString *str_shop_price = [lxStringUtility stringTrim:[NSString stringWithFormat:@"%10.2f", lxentity.shop_price]];
    if (YES == [lxStringUtility isNil:str_shop_price]) {
        str_shop_price = @"0";
    }
    _lb_shop_price.text = [NSString stringWithFormat:@"%@%@", str_shop_price, money];
    _lb_shop_price.textColor = [UIColor redColor];
    
    NSString *str_promote_price = [lxStringUtility stringTrim:[NSString stringWithFormat:@"%10.2f", lxentity.promote_price]];
    if (YES == [lxStringUtility isNil:str_promote_price]) {
        str_promote_price = @"0";
    }
    _lb_promote_price.text = [NSString stringWithFormat:@"%@%@", str_promote_price, money];
    _lb_promote_price.textColor = [UIColor redColor];
    
    NSString *comment_rank_img = lxentity.comment_rank_img;
    
    if (nil != comment_rank_img) {
        NSString *str_url = [NSString stringWithFormat:@"%@%@", web_root, comment_rank_img];
        
        [_image_comment_rank_img loadImageFromURL:str_url];
    }
    return YES;
}

- (void)dealloc {
    [_img_goods_view release];
    [_lb_market_price_lb release];
    [_lb_shop_price_lb release];
    [_lb_promote_price_lb release];
    [_lb_comment_rank_lb release];
    [_lb_market_price release];
    [_lb_shop_price release];
    [_lb_promote_price release];
    [_image_comment_rank_img release];
    [super dealloc];
}

@end
